package cn.tedu.spotms.server.service.impl;

import cn.tedu.spotms.server.ex.ServiceException;
import cn.tedu.spotms.server.mapper.SightMapper;
import cn.tedu.spotms.server.pojo.dto.SightDTO;
import cn.tedu.spotms.server.pojo.entity.Sight;
import cn.tedu.spotms.server.pojo.vo.SightSimpleVO;
import cn.tedu.spotms.server.service.ISightService;
import cn.tedu.spotms.server.web.State;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class SightServiceImpl implements ISightService {
    @Autowired
    SightMapper sightMapper;

    //添加新的景点信息
    @Override
    public void addNewSight(SightDTO sightDTO) {
        log.trace("开始添加新的景点");
        //根据标题查询数据库判断景区是否已存在
        String title=sightDTO.getTitle();
        SightSimpleVO sightVo = sightMapper.getByTitle(title);
        //存在则抛出异常:景点已存在
        if (sightVo!=null){
            log.warn("景点已存在,抛出异常");
            throw new ServiceException(State.ERR_SIGHT_NAME_DUPLICATE,"此景点已存在!");
        }
        //不存在插入新数据
        // 创建Sight对象
        Sight sight=new Sight();
        // 调用BeanUtils.copyProperties()将参数对象中的属性值复制到sight对象中,并补全相应数据
        BeanUtils.copyProperties(sightDTO,sight);
        //补全Sight对象中的属性值：viewCount >>> 默认0
        sight.setViewCount(0);
        //补全Merchant对象中的属性值：gmtCreate/gmtModified >>> 默认当前系统时间
        sight.setGmtCreate(LocalDateTime.now());
        sight.setGmtModified(LocalDateTime.now());
        sightMapper.addSight(sight);

    }

    @Override
    public void deleteSightById(Integer id) {
        //根据id查询景点是否存在
        SightSimpleVO infoById = sightMapper.getInfoById(id);
        log.info("查询到的景点信息为:{}",infoById);
        //不存在,抛出异常
        if (infoById==null){
            log.warn("景点:{},不存在,即将抛出异常",infoById);
            throw new ServiceException(State.ERR_SIGHT_NOT_FOUND,"删除失败,景点不存在!");
        }
        //存在,调用数据库删除景点
        int raws = sightMapper.deleteSightById(id);
        //判断返回受影响行数是否为1
        if (raws!=1){
            //若不等于1,抛出异常
            log.warn("删除失败,即将抛出异常");
            throw new ServiceException(State.ERR_BAD_REQUEST,"删除失败,服务器繁忙");
        }
        log.debug("删除景点成功 ");
    }

    //修改景点信息
    @Override
    public void updateSightInfo(SightDTO sightDTO) {
        log.trace("开始修改");
        //根据管理员输入景点信息的id判断是否已存在
        Integer id = sightDTO.getId();
        log.debug("管理员要修改的景点id为{}",id);
        SightSimpleVO infoById = sightMapper.getInfoById(id);
        if (infoById==null){
        //查询为空,抛出异常
            throw new ServiceException(State.ERR_SIGHT_NOT_FOUND,"景点不存在!");
        }
        String sightName=sightDTO.getTitle();
        SightSimpleVO ResultByTitle = sightMapper.getByTitle(sightName);
        if (ResultByTitle!=null&&!id.equals(ResultByTitle.getId())){
            //根据管理员输入的景点标题查询数据库是否已有对应数据且id是否与修改的数据一致,不一致抛出异常
            log.warn("景点{}修改失败,名称已被占用",infoById.getTitle());
            throw new ServiceException(State.ERR_SIGHT_NAME_DUPLICATE,"修改失败,景点"+sightDTO+"已存在!");
        }
        //修改景点修改时间为当前系统时间
        sightMapper.updateGmtModifiedById(id,LocalDateTime.now());
        //将修改后的数据更新到数据库
        sightMapper.updateSightInfo(sightDTO);
        log.debug("修改景点信息{}完成",sightDTO);
    }

    //根据id展示景点信息
    @Override
    public List<SightSimpleVO> listAllInfo() {
        List<SightSimpleVO> sightSimpleVOS = sightMapper.listAllInfo();
        log.info("所有景点信息为{}",sightSimpleVOS);
        return sightSimpleVOS;
    }
}
